<script setup lang="ts">
import { ref, computed } from 'vue';
import {ArrowLeft, ArrowRight} from "@element-plus/icons-vue";
import {CarouselVO} from "../../../../generated";

const props = defineProps<{
  recommendations: CarouselVO[]
}>();

const currentIndex = ref(0);

// 根据屏幕宽度获取每页显示数量
const getItemsPerView = () => {
  if (window.innerWidth < 500) return 1;
  if (window.innerWidth < 768) return 2;
  return 3;
};

const displayItems = computed(() => {
  const start = currentIndex.value;
  const itemsToShow = getItemsPerView();
  const remainingItems = props.recommendations.length - start;
  return props.recommendations.slice(start, start + Math.min(itemsToShow, remainingItems));
});

const canNext = computed(() => {
  return currentIndex.value + getItemsPerView() < props.recommendations.length;
});

const canPrev = computed(() => {
  return currentIndex.value > 0;
});

const next = () => {
  if (canNext.value) {
    currentIndex.value += getItemsPerView();
  }
};

const prev = () => {
  if (canPrev.value) {
    currentIndex.value -= getItemsPerView();
  }
};
</script>

<template>
  <div class="recommendation-section">
    <div class="section-header">
      <h2>推荐</h2>
      <div class="nav-buttons">
        <button
          class="nav-button"
          @click="prev"
          :disabled="!canPrev"
        >
          <el-icon><ArrowLeft /></el-icon>
        </button>
        <button
          class="nav-button"
          @click="next"
          :disabled="!canNext"
        >
          <el-icon><ArrowRight /></el-icon>
        </button>
      </div>
    </div>
    <div class="recommendation-container">
      <transition-group
        name="recommendation-slide"
        tag="div"
        class="recommendation-group"
      >
        <div
          v-for="item in displayItems"
          :key="item.id"
          class="recommendation-item"
        >
          <img :src="item.imageUrl">
        </div>
      </transition-group>
    </div>
  </div>
</template>

<style scoped>
.recommendation-section {
  width: 100%;
  margin-bottom: 32px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-header h2 {
  font-size: 20px;
  font-weight: 500;
  margin: 0;
}

.nav-buttons {
  display: flex;
  gap: 8px;
}

.nav-button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background-color: #f5f5f5;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.nav-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.nav-button:not(:disabled):hover {
  background-color: #e0e0e0;
}

.recommendation-container {
  width: 100%;
  overflow: hidden;
  border-radius: 8px;
}

.recommendation-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  width: 100%;
  position: relative;
}

.recommendation-item {
  height: 135px;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
}

.recommendation-item img {
  width: 100%;
  height: 100%;
  border-radius: 8px;
}

.recommendation-slide-move {
  transition: all 0.5s ease;
}

.recommendation-slide-enter-active,
.recommendation-slide-leave-active {
  transition: all 0.5s ease;
}

.recommendation-slide-enter-from {
  opacity: 0;
  transform: translateX(100%);
}

.recommendation-slide-leave-to {
  opacity: 0;
  transform: translateX(-100%);
}

.recommendation-slide-leave-active {
  position: absolute;
}

/* 添加响应式样式 */
@media (max-width: 768px) {
  .recommendation-group {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .recommendation-item {
    height: 140px;
  }
}

@media (max-width: 500px) {
  .recommendation-group {
    grid-template-columns: 1fr;
  }
  
  .recommendation-item {
    height: 120px;
  }
}
</style>
